package com.nobleoxtechnology.weatherSMS;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences;
import android.telephony.SmsManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Bundle;

public class weatherSMS extends Activity
{	
	public int treeBranch = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        final SharedPreferences pref = getSharedPreferences(null, 0);
		if (pref.getBoolean("acceptedTerms", false) == false)
        {
        	AlertDialog.Builder termsOfUseWarning = new AlertDialog.Builder(this);
        	termsOfUseWarning.setPositiveButton("I Agree", new OnClickListener()
        	{
        		@Override
        		public void onClick(DialogInterface dialog, int which)
        		{
        			Editor savePref = pref.edit();
        			savePref.putBoolean("acceptedTerms", true);
        			savePref.commit();
        		}
        	});

        	termsOfUseWarning.setNegativeButton("I Disagree", new OnClickListener()
        	{
        		@Override
        		public void onClick(DialogInterface dialog, int which)
        		{
        			finish();
        		}
        	});
        	termsOfUseWarning.setCancelable(false);
        
	        termsOfUseWarning.setTitle("Terms of Use Agreement");
	        termsOfUseWarning.setMessage("This application is provided \"as is\" and without warranty; there is no guarantee that this will function as intended by the user, or by the author.  This application uses SMS texts for retrieving weather information courtesy of the free SMS text service provided by Google SMS Applications.  If you do not have an unlimited texting plan with your cellular provider, it is highly suggested that you do NOT use this application.  By agreeing to these terms, you're agreeing that the author of this application, David Magnotti, takes no responsibility and is not liable under ANY condition concerning the application (Including, but not limited to: financial loss, physical harm, or emotional distress).  If you agree to the these terms, and would like to use this application, click on \"I Agree\".");
	        termsOfUseWarning.show();
        }
        
        // Consider reverting to main screen
        setContentView(R.layout.main);
        
        Button buttonUpdate = (Button) findViewById(R.id.buttonUpdateNow);
        final Context context = this;
        buttonUpdate.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		try
        		{
        			String phoneNumber = pref.getString("phone", "466453");
        	    	String msg = pref.getString("send", "%zip% weather").replace("%zip%", pref.getString("zip", "11110"));
                	SmsManager sms = SmsManager.getDefault();
                	
                	sms.sendTextMessage(phoneNumber, null, msg, null, null);
                	
                	Toast update = Toast.makeText(context, "Updating . . .", Toast.LENGTH_LONG);
               		update.show();
        		}
        		catch (Exception e)
        		{
        			Toast update = Toast.makeText(context, "Error occurred: " + e.getMessage(), Toast.LENGTH_LONG);
        			update.show();
        		}
        	}
        });
    }
    
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.settings:
			treeBranch = 1;
			setContentView(R.layout.settings);
			setTitle("weatherSMS - Settings");
			
			ListView lvSettings = (ListView) findViewById(R.id.listViewSettings);
			lvSettings.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , 
					getResources().getStringArray(R.array.settings)));
			
			lvSettings.setOnItemClickListener(new Settings(this, this, 
					getSharedPreferences(null, 0), this));
			break;
		}
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (treeBranch == 2)
			{
				treeBranch = 1;
				setContentView(R.layout.settings);
				setTitle("weatherSMS - Settings");
				
				ListView lvSettings = (ListView) findViewById(R.id.listViewSettings);
				lvSettings.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
						getResources().getStringArray(R.array.settings)));
					
				lvSettings.setOnItemClickListener(new Settings(this, this, 
						getSharedPreferences(null, 0), this));
				
				return true;
			}
			else if (treeBranch == 1)
			{
				treeBranch = 0;
				setTitle("weatherSMS");
				setContentView(R.layout.main);
				
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	} 
}